package cn.net.shuxing.yifu.common.front.controller;

import cn.net.shuxing.yifu.common.bean.CoinType;
import cn.net.shuxing.yifu.common.bean.Country;
import cn.net.shuxing.yifu.common.dto.BaseDto;
import cn.net.shuxing.yifu.common.service.CountryService;
import cn.net.shuxing.yifu.common.util.HttpClientUtil;
import cn.net.shuxing.yifu.common.util.JedisUtil;
import cn.net.shuxing.yifu.common.util.RedisKeyCreator;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import cn.net.shuxing.yifu.common.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.*;

@RestController
public class ExchangeRateController {

    @Autowired
    private JedisUtil jedisUtil;

    @Autowired
    private CountryService countryService;

    /** 新增 **/
    /**
     * 获取汇率
     *
     * @return
     */
    @RequestMapping("/front/info/exchangeRate")
    public BaseDto getExchangeRate() {
        //Jedis //jedis = jedisUtil.getJedis();
        String rate = jedisUtil.get(RedisKeyCreator.exchangeRate());
        if (StringUtils.hasText(rate)) {
            //jedisUtil.close();
            return BaseDto.ok(rate);
        }
        Map<String, String> map = new LinkedHashMap<>();
        map.put("app", "finance.rate");
        map.put("scur", "USD");
        map.put("tcur", "CNY");
        map.put("appkey", "41588");
        map.put("sign", "ff16e33fece7cfd835f2de67d69ad4d6");
        String resultJson = HttpClientUtil.doGet("http://api.k780.com/", map);
        JSONObject jsonObject = JSON.parseObject(resultJson);
        JSONObject result = (JSONObject) jsonObject.get("result");
        rate = result.get("rate").toString();
        jedisUtil.set(RedisKeyCreator.exchangeRate(),rate);
        jedisUtil.expire(RedisKeyCreator.exchangeRate(),60);
//        jedisUtil.setex(RedisKeyCreator.exchangeRate(), 60, rate);
        //jedisUtil.close();
        return BaseDto.ok(rate);
    }

    @RequestMapping("/front/info/ethExchangeRate")
    public BaseDto getETHExchangeRate() {
        //Jedis //jedis = jedisUtil.getJedis();
        String rate = jedisUtil.get(RedisKeyCreator.ethExchangeRate());
        if (StringUtils.hasText(rate)) {
            //jedisUtil.close();
            return BaseDto.ok(rate);
         }else {
            return BaseDto.ok(1690);
        }
    }
    @RequestMapping("/front/info/country")
    public BaseDto getCountry(String name,
                              @RequestParam(defaultValue = "1", value = "pageNum") int pageNum,
                              @RequestParam(defaultValue = "15", value = "pageSize") int pageSize
                              )throws UnsupportedEncodingException {
        System.out.println("解析前coinTypeName"+name);
        String newName ="";
        if (name!=null){
            newName = new String(name.getBytes("ISO-8859-1"), "UTF-8");
        }
//        System.out.println("解析后coinTypeName"+newName);
        BaseDto baseDto = new BaseDto();
        List<Country> coinTypes = countryService.searchByName(newName,pageNum,pageSize);
        baseDto.setData(coinTypes);
        baseDto.setCode(0);
        return baseDto;
    }
}
